package com.atguigu.gulimall.order.config;

import com.atguigu.gulimall.order.entity.OrderEntity;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 使用更方便的方式创建队列、交换机以及他们的绑定关系
 * 只要作为组建注册进了容器，队列、交换机以及他们的绑定关系就会被SpringBoot自动创建
 */
@Configuration
public class MyMqConfig {

    //模拟延时队列
    @RabbitListener(queues = {"order.release.order.queue"})
    public void listener(OrderEntity orderEntity, Channel channel, Message message) throws IOException {
        System.out.println("收到过期的订单信息，准备关闭订单："+orderEntity.getOrderSn());
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }




    /**
     * 这个是死信队列
     * @return
     */
    @Bean
    public Queue orderDelayQueue(){

        /**
         * Queue(String name,
         *      boolean durable,
         *      boolean exclusive,
         *      boolean autoDelete,
         *      @Nullable Map<String, Object> arguments)
         */
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-dead-letter-exchange","order-event-exchange");
        arguments.put("x-dead-letter-routing-key","order.release.order");
        arguments.put("x-message-ttl",30000);
        Queue queue = new Queue("order.delay.queue",true,false,false,arguments);
        return queue;
    }

    /**
     * 这个是最后接收消息的队列。就是一个普通的队列
     * @return
     */
    @Bean
    public Queue orderRaleaseQueue(){
        Queue queue = new Queue("order.release.order.queue",true,false,false);
        return queue;
    }

    /**
     * 用于秒杀的队列,用于做秒杀流量削峰
     * @return
     */
    @Bean
    public Queue orderSeckillOrderQueue(){

        return new Queue("order.seckill.order.queue",true,false,false);
    }


    @Bean
    public Exchange orderEventExchange(){
        /**
         * TopicExchange(String name,
         *              boolean durable,
         *              boolean autoDelete,
         *              Map<String, Object> arguments)
         */
        TopicExchange topicExchange = new TopicExchange("order-event-exchange",true,false);
        return topicExchange;
    }

    @Bean
    public Binding orderCreateOrderBinding(){
        /**
         * Binding( String destination,
         *          DestinationType destinationType,
         *          String exchange,
         *          String routingKey,
         *          @Nullable Map<String, Object> arguments)
         */
        Binding binding = new Binding("order.delay.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.create.order",
                null);
        return binding;
    }

    @Bean
    public Binding orderReleaseOrderBingding(){
        Binding binding = new Binding("order.release.order.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.release.order",
                null);
        return binding;
    }

    /**
     * 交换机与秒杀削峰队列的绑定关系
     */
    @Bean
    public Binding orderSeckillOrderBinding(){
        /**
         * Binding(
         *      String destination,
         *      DestinationType destinationType,
         *      String exchange,
         *      String routingKey,
         *      @Nullable Map<String, Object> arguments)
         */
        return new Binding("order.seckill.order.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.seckill.order",
                null);
    }
}
